package com.turing.post.organization.impl;

import java.math.BigInteger;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.IncomeBranch;
import com.turing.post.organization.util.IncomeBranchBusiness;
import com.turing.post.organization.util.IncomeBranchDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.organization.util.SysBranch;
import com.turing.post.organization.util.SysBranchDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Jan 10, 2013
 */

/**
 * @author zxp
 * @Dec 20, 2012
 */
@Stateless(mappedName = "organization.impl.IncomeBranchBusinessImpl")
@Remote
@Local
public class IncomeBranchBusinessImpl implements IncomeBranchBusiness {

	@EJB(mappedName = "organization.impl.IncomeBranchDaoImpl")
	private IncomeBranchDao incomeBranchDaoImpl;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;
   
	@EJB(mappedName = "organization.impl.SysBranchDaoImpl")
	private SysBranchDao sysBranchDaoImpl;//
	
	@Override
	public void insertIncomeBranchInfo(PCPSWebInput webInput,
			IncomeBranch incomeBranch) throws PCPSApplicationException {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"登录操作员归属机构号为空!");
		}
		if (PCPSUtil.isEmpty(incomeBranch.getIssueBranchNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"发卡机构号为空!");
		}
		// 验证是不是发卡机构
		SysBranch branchform = sysBranchDaoImpl.getSysBranchByBranchNo(incomeBranch.getIssueBranchNo());
		if (!branchform.getBranchType().equals(SysBranch.SYSBRANCH_ISSUEBRANCH)) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"机构不是发卡机构!");
		}
		if (PCPSUtil.isEmpty(incomeBranch.getIncomeBranchName())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"IncomeBranchName");
		}
		incomeBranch.setIncomeBranchId(commonDaoImpl.getUniqueIntegerKey());
		incomeBranch.setIssueCompanyNo(webInput.getOperBranchNo().substring(0,
				6));
		incomeBranchDaoImpl.insertIncomeBranch(incomeBranch);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "创建发卡商渠道收益机构", incomeBranch,
				SysOperLog.operType_Add, "IncomeBranchName:"
						+ incomeBranch.getIncomeBranchName());
	}

	@Override
	public void updateIncomeBranchInfo(PCPSWebInput webInput,
			IncomeBranch incomeBranch) throws PCPSApplicationException {
		IncomeBranch _incomeBranch = incomeBranchDaoImpl
				.getIncomeBranchByIncomeBranchId(incomeBranch
						.getIncomeBranchId());
		if (PCPSUtil.isEmpty(incomeBranch.getIssueBranchNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"发卡机构号为空!");
		}
		// 验证是不是发卡机构
		SysBranch branchform = sysBranchDaoImpl.getSysBranchByBranchNo(incomeBranch.getIssueBranchNo());
		if (!branchform.getBranchType().equals(SysBranch.SYSBRANCH_ISSUEBRANCH)) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"机构不是发卡机构!");
		}
		_incomeBranch.setIncomeBranchName(incomeBranch.getIncomeBranchName());
		_incomeBranch.setRemark(incomeBranch.getRemark());
		_incomeBranch.setIssueBranchNo(incomeBranch.getIssueBranchNo());
		incomeBranchDaoImpl.updateIncomeBranch(_incomeBranch);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "修改发卡商渠道收益机构", incomeBranch,
				SysOperLog.operType_Edit, "IncomeBranchName:"
						+ incomeBranch.getIncomeBranchName());
	}
	/**
	 * 根据id删除收益机构
	 */
	public void deleteIncomeBranch(PCPSWebInput webInput,String incomeBranchid)throws PCPSApplicationException{
		if (webInput==null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"机构为空!");
		}
		if (PCPSUtil.isEmpty(incomeBranchid)) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"收益机构id为空!");
		}
		// 验证是不是发卡机构
		IncomeBranch incomeBranch = incomeBranchDaoImpl.getIncomeBranchByIncomeBranchId(new BigInteger(incomeBranchid));
		if (incomeBranch==null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"收益机构不存在!");
		}
		if(incomeBranchDaoImpl.isIncomeBranch(webInput.getOperBranchNo().substring(0,6), incomeBranchid)>0){
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
			       "删除失败!收益机构可能已被引用，不能删除!");
		}
		incomeBranchDaoImpl.deleteIncomeBranch(incomeBranch);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "删除发卡商渠道收益机构", incomeBranch,
				SysOperLog.operType_Edit, "IncomeBranchName:"
						+ incomeBranch.getIncomeBranchName());
	}
	private void insertRecordingOperationLog(PCPSWebInput webInput,
			String logName, IncomeBranch obj, String operType,
			String lastFieldName) throws PCPSApplicationException {
		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
		if (!PCPSUtil.isEmpty(logRemark)) {
			// 插入数据库日志
			SysOperLog sysOperLog = new SysOperLog();
			if (webInput != null && webInput.getOperId() != null) {
				sysOperLog.setOperId(webInput.getOperId());
				sysOperLog.setOperType(operType);
				sysOperLog.setLogName(logName);
				if (webInput != null) {
					sysOperLog.setIpAddress(webInput.getIpAddress());
				}
				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
					sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
							.substring(0, 6));
					sysOperLog.setBranchNo(webInput.getOperBranchNo());
				}
				sysOperLog.setRemark(logRemark);
				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
			}
		}
	}
}